//index.js
const app = getApp()
const Url = app.url
const util = require('../../../utils/util.js')

Page({
	data: {
		userInfo: {},
		hasUserInfo: false,
		canIUse: wx.canIUse('button.open-type.getUserInfo'),
		customerWxId: '', // openId
		couponDetailInfo: {}, // 优惠券详情
		couponLogo: '', // 优惠券logo
	},

	onLoad: function (options) {
		this.setCustomerWxId()
		console.log('options:', options)
		if (app.globalData.userInfo) {
			this.setData({
				userInfo: app.globalData.userInfo,
				hasUserInfo: true,
			})
			console.log(this.data.userInfo, 'this.data.userInfo')
		} else if (this.data.canIUse) {
			// 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
			// 所以此处加入 callback 以防止这种情况
			app.userInfoReadyCallback = (res) => {
				this.setData({
					userInfo: res.userInfo,
					hasUserInfo: true,
				})
			}
		} else {
			// 在没有 open-type=getUserInfo 版本的兼容处理
			wx.getUserInfo({
				success: (res) => {
					console.log(res, 'res')
					app.globalData.userInfo = res.userInfo
					this.setData({
						userInfo: res.userInfo,
						hasUserInfo: true,
					})
				},
			})
		}
		this.getCouponDetail(options)
	},

	// 跳转到更多门店页面
	jumpToAllStore: function () {
		wx.navigateTo({
			url: '../../my/allStore/index',
		})
	},

	// 设置openid
	setCustomerWxId: function () {
		if (!!wx.getStorageSync('openid')) {
			this.setData({
				customerWxId: wx.getStorageSync('openid'),
			})
		} else {
			wx.login({
				success: (res) => {
					wx.setStorageSync('codeMes', res.code)
					util.getOpenId(Url.login)
					this.setData({
						customerWxId: wx.getStorageSync('openid'),
					})
				},
			})
		}
	},

	// 跳转到折扣券详情二维码页面
	jumpToCouponQrCode: function (event) {
		wx.showLoading({
			title: '加载中',
			mask: true,
		})
		wx.request({
			url:
				Url.receiveCoupon +
				`?customerWxId=${this.data.customerWxId}&couponId=${this.data.couponDetailInfo.couponId}&wxName=${this.data.userInfo.nickName}&avatar=${this.data.userInfo.avatarUrl}`,
			method: 'GET',
			data: {},
			header: {
				'content-type': 'application/json',
			},
			success: function (res) {
				wx.hideLoading()
				console.log(res, 'res')
				if (res.data.code == '200') {
					console.log(res.data, 'res.data')
					const couponNum = res.data.model.couponNum
					const couponName = event.currentTarget.dataset.name
					const couponLogo = event.currentTarget.dataset.logo
					console.log(couponNum, couponName, couponLogo, '1111')
					wx.navigateTo({
						url: `../couponQrCode/index?couponNum=${couponNum}&couponName=${couponName}&couponLogo=${couponLogo}`,
					})
				} else {
					wx.showToast({
						title: res.data.msg,
						icon: 'none',
						duration: 2000,
					})
				}
			},
			fail: function (err) {
				wx.hideLoading()
				console.log(err)
				wx.showToast({
					title: err.errMsg,
					icon: 'none',
					duration: 2000,
				})
			},
		})
	},

	// 获取用户信息
	getUserInfo: function (e) {
		console.log(e)
		app.globalData.userInfo = e.detail.userInfo
		this.setData({
			userInfo: e.detail.userInfo,
			hasUserInfo: true,
		})
	},

	// 获取优惠券详情
	getCouponDetail: function (options) {
		let that = this
		if (options && options.couponId) {
			wx.showLoading({
				title: '加载中',
				mask: true,
			})
			wx.request({
				url: Url.queryCouponDetail + `?couponId=${options.couponId}`,
				method: 'GET',
				data: {},
				header: {
					'content-type': 'application/json',
				},
				success: function (res) {
					wx.hideLoading()
					console.log(res, 'res')
					if (res.data.code == '200') {
						let response = res.data.model
						console.log(response, 'response')
						response.config = JSON.parse(response.config)
						that.setData({
							couponDetailInfo: response,
						})
						that.getCouponLogo(that.data.couponDetailInfo.logo)
					} else {
						wx.showToast({
							title: res.data.msg,
							icon: 'none',
							duration: 2000,
						})
					}
				},
				fail: function (err) {
					wx.hideLoading()
					console.log(err)
					wx.showToast({
						title: err.errMsg,
						icon: 'none',
						duration: 2000,
					})
				},
			})
		} else if (options && options.couponStr) {
			const couponDetailInfo = JSON.parse(options.couponStr)
			this.setData({
				couponDetailInfo,
			})
			that.getCouponLogo(that.data.couponDetailInfo.logo)
		}
	},

	// 获取优惠券logo
	getCouponLogo: function (logo) {
		const couponLogo = `https://www.wqychina.com/wkd${logo}`
		this.setData({
			couponLogo,
		})
	},
})
